﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
using TranslatorService;

namespace GlosbeTranslator
{
	#pragma warning disable 1998
	public class GlosbeTranslator : ITranslatorService
	{
		public TranslatorService.TranslatorServiceCapabilities ServiceCapabilities
		{
			get
			{
				return TranslatorServiceCapabilities.DefinesWords |
					TranslatorServiceCapabilities.PlainText |
					TranslatorServiceCapabilities.TranslatesWords;
			}
		}

		public string ServiceName
		{
			get { return "Glosbe"; }
		}

		public Uri ServiceURI
		{
			get { return new Uri("http://glosbe.com/a-api"); }
		}

		public string[] AuthenticationParameterDetail
		{
			get { return null; }
		}

		public async Task<bool> AuthenticateAsync(params string[] options)
		{
			return true;
		}

		public async Task<Dictionary<string, string>> GetSupportedLanguagesAsync(string locale = "en")
		{
			var dict = new Dictionary<string, string>(8129);
			var rdr = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("GlosbeTranslator.Resources.iso-639-3_Name_Index.txt"));
			string ln;
			while ((ln = rdr.ReadLine()) != null)
			{
				string[] items = ln.Split('\t');
				dict.Add(items[0], items[1]);
			}
			return dict;
		}

		[DataContract]
		class RootObject
		{
			[DataMember]
			public string result { get; set; }
			[DataMember]
			public object authors { get; set; }
			[DataMember]
			public string dest { get; set; }
			[DataMember]
			public string phrase { get; set; }
			[DataMember]
			public List<Tuc> tuc { get; set; }
			[DataMember]
			public string from { get; set; }

			[DataContract]
			public class Tuc
			{
				[DataMember]
				public List<int> authors { get; set; }
				[DataMember]
				public object meaningId { get; set; }
				[DataMember]
				public List<Meaning> meanings { get; set; }
				[DataMember]
				public Phrase phrase { get; set; }

				[DataContract]
				public class Phrase
				{
					[DataMember]
					public string text { get; set; }
					[DataMember]
					public string language { get; set; }
				}

				[DataContract]
				public class Meaning
				{
					[DataMember]
					public string text { get; set; }
					[DataMember]
					public string language { get; set; }
				}
			}
		}

		public async Task<string> TranslateAsync(string sourceLang, string destLang, string value, InputFormat format = InputFormat.Text)
		{
			try
			{
				var resp = await CallMethodAsync<RootObject>("translate", "from", sourceLang, "dest", destLang, "format", "json", "phrase", value, "pretty", "true", "tm", "false");
				return resp.tuc[0].phrase.text;
			}
			catch { }
			return string.Empty;
		}

		public async Task<string> DetectLanguageAsync(string value)
		{
			throw new NotImplementedException();
		}

		public async Task<T> CallMethodAsync<T>(string methodName, params string[] parameters) where T : class
		{
			return await HttpUtility.CallJsonMethodAsync<T>("http://glosbe.com/gapi/", methodName, (string)null, parameters);
		}
	}
}
